package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.Floor;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public List<NursingPlanVo> getListAll() {
        return nursingPlanMapper.getListAll();
    }

    @Override
    @Transactional
    public void insert(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
//        System.out.println(nursingPlanDto);
//        System.out.println(nursingPlan);
        nursingPlanMapper.insert(nursingPlan);
        List<NursingProjectPlanDto> list = nursingPlanDto.getNursingProjectPlanDtoList();
        for (NursingProjectPlanDto nursingProjectPlanDto : list){
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtils.copyProperties(nursingProjectPlanDto, nursingProjectPlan);
            nursingProjectPlan.setPlanId(Math.toIntExact(nursingPlan.getId()));
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        }
        System.out.println(nursingPlanDto);
        System.out.println(list);
    }

    @Override
    public PageResponse<NursingPlanVo> getListByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> byPage = nursingPlanMapper.findByPage(name,status);
        System.out.println(byPage);
        return PageResponse.of(byPage, NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getById(Integer id) {
        return nursingPlanMapper.selectById(id);
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }

    @Override
    public void delete(Integer id) {
        nursingPlanMapper.delete(id);
    }
}
